//
//  DynamicEditViewController.m
//  QuanZiChat
//
//  Created by 张洪林 on 15/11/26.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "DynamicEditViewController.h"
#import "DynamicEditViewCell.h"
#import "JZAlbumManager.h"
#import "XHPhotographyHelper.h"

@interface DynamicEditViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *superView;
@property(nonatomic,strong)UIImage *addBtnImage;
@property(nonatomic,strong)UIImageView *imageView;
@property (strong,nonatomic) XHPhotographyHelper* photographyHelper;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *superViewHeight;



@end

@implementation DynamicEditViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.superViewHeight.constant =(200 + ((self.imageArray.count - 1) / 4) * 90);
    [self.collectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = [UIColor lightGrayColor];
    [self.imageArray addObject:self.addBtnImage];
    
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DynamicEditViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor blueColor];
    cell.image = self.imageArray[indexPath.item];
    return  cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"选中 section:%ld,item:%ld",indexPath.section,indexPath.item);
    if (indexPath.section == 0 && indexPath.item == self.imageArray.count - 1) {
        //初始化图片选择器
        UIImagePickerController *IPC = [[UIImagePickerController alloc]init];
        //设置图片来源类型
        IPC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //设置代理
        IPC.delegate = self;
        //可编辑
        IPC.allowsEditing = YES;
        //模态
        [self showDetailViewController:IPC sender:nil];
    }
    
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //获取选中的图片
    UIImage  * image = info[UIImagePickerControllerEditedImage];
    [self.imageArray removeObject:self.addBtnImage];
    [self.imageArray addObject:image];
    [self.imageArray addObject:self.addBtnImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];
    //[self.collectionView reloadData];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    //点击取消按钮时返回主界面
    [picker dismissViewControllerAnimated:YES completion:NULL];
}





- (IBAction)didClickSend:(id)sender {
    [self.imageArray removeObject:self.addBtnImage];
    if(self.textView.text.length!=0 || self.imageArray.count!=0){
        WEAKSELF
        [self showProgress];
        [self runInGlobalQueue:^{
            NSError* error;
            [[JZAlbumManager manager] createAlbumWithText:self.textView.text photos:self.imageArray error:&error];
            [weakSelf runInMainQueue:^{
                [weakSelf hideProgress];
                if(error==nil){
                    [_albumVC refresh];
                    [weakSelf dismiss];
                }else{
                    [weakSelf alertError:error];
                }
            }];
        }];
    }else{
        [self alert:@"请完善内容"];
    }
    
}
- (IBAction)didClickCancel:(id)sender {
    [self dismiss];
}
-(void)dismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
}










-(NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [NSMutableArray new];
    }
    return _imageArray;
}
-(UIImage *)addBtnImage{
    if (!_addBtnImage) {
        _addBtnImage = [UIImage imageNamed:@"iconfont-jia01"];
    }
    return _addBtnImage;
}
-(XHPhotographyHelper*)photographyHelper{
    if(_photographyHelper==nil){
        _photographyHelper=[[XHPhotographyHelper alloc] init];
    }
    return _photographyHelper;
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
